package com.wealth.system.web.controller;

import iaas.upload.ApiUploadService;
import iaas.utils.ResultObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import com.wealth.system.entity.Corporation;
import com.wealth.system.service.ICorpService;
import com.wealth.wechat.entity.WechatConfig;
import com.wealth.wechat.service.WechatConfigService;

/**
 * 
 * @author chan
 */
@Controller
@RequestMapping
public class BaseController {

	public Long corpid = 2L;

	public Long memberid = 3174L;

	
	
	@Autowired
	private ApiUploadService apiUploadService;
	@Autowired
	private WechatConfigService wechatConfigService;
	@Autowired
	private ICorpService corpService;
	
	
	@ModelAttribute
	public void init(Model model, HttpSession session,
			HttpServletRequest request) throws Exception {
		// 过滤器处理

		if(session.getAttribute("memberid")!=null){
			Long tempid = Long.valueOf(session.getAttribute("memberid").toString());
			request.setAttribute("memberid", tempid);
			this.memberid = tempid;
			
		}
		Long corpid = Long.valueOf(session.getAttribute("corpid").toString());
		this.corpid = corpid;
		request.setAttribute("corpid", corpid);
		request.setAttribute("path", session.getAttribute("path").toString());
		request.setAttribute("resources", session.getAttribute("resources").toString());
		share(model);
	}

	
	public void share(Model model) throws Exception{
		String appid=wechatConfigService.getWechatConfigByCorpid(corpid)==null?null:wechatConfigService.getWechatConfigByCorpid(corpid).getWxappid();
		Corporation corps=corpService.get(corpid);
		model.addAttribute("appid", appid);
		model.addAttribute("corpid", corpid);
		model.addAttribute("url",corps.getShareurl());
		model.addAttribute("title", corps.getTitle());
		model.addAttribute("imageurl", corps.getPicurl());
		model.addAttribute("content", corps.getContent());
	}
	/**
	 * 上传图片 --------功能/参数说明---------- 该方法不会压缩图片 width = 0 height = 0
	 */
	@RequestMapping(value = "/upload/pic", method = RequestMethod.POST)
	@ResponseBody
	public ResultObject uplaodProductPic(
			DefaultMultipartHttpServletRequest request) {
		MultipartFile file = request.getFile("file");
		// 这里设置不能大于5M，springmvc配置最大是10M
		System.out.println("文件大小：" + file.getSize());
		if (file.getSize() >= 1024 * 1024 * 5) {
			return new ResultObject("500", "文件不能大于5M", "");
		}

		String filepath = apiUploadService.uploadPictureFile(file, "", 0, 0,
				false, false, request);
		return new ResultObject("200", "", filepath);
	}
	
}
